using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    [SerializeField] private Board board;
    [SerializeField] private ScoreManager scoreManager;
    [SerializeField] private GameObject gameOverPanel;
    [SerializeField] private Button restartButton;
    [SerializeField] private Button quitButton;
    private void Start()
    {
        if(board!=null)
        {
            board.onGameOver+= HandleGameOver;
        }
        if(restartButton != null)
        {
            restartButton.onClick.AddListener(RestartGame);
        }
        if(quitButton != null)
        {
            quitButton.onClick.AddListener(QuitGame);
        }
        if(gameOverPanel!=null)
        {
            gameOverPanel.SetActive(false);
        }
    }
    private void HandleGameOver()
    {
        if(gameOverPanel!=null)
        {
            gameOverPanel.SetActive(true);
        }
    }
    public void RestartGame()
    {
        Debug.Log("重启游戏");
        if(gameOverPanel!=null)
        {
            gameOverPanel.SetActive(false);
        }
        if(scoreManager!=null)
        {
            scoreManager.ResetScore();
            Debug.Log("重置分数");
        }
        if(board!=null)
        {
            board.RestartGame();
        }
    }
    private void QuitGame()
    {
        #if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
        #else
            Application.Quit();
        #endif
    }
    private void OnDestroy()
    {
        if(board!=null)
        {
            board.onGameOver-=HandleGameOver;
        }
        if(restartButton!=null)
        {
            restartButton.onClick.RemoveListener(RestartGame);
        }
        if(quitButton!=null)
        {
            quitButton.onClick.RemoveListener(QuitGame);
        }
    }
}
